package rexsee.core.browser;

import rexsee.core.browser.clazz.FilterListener;
import rexsee.core.browser.clazz.JavascriptInterface;
import rexsee.core.browser.clazz.PregFilterListener;
import rexsee.core.browser.clazz.PregPair;

public class RexseePregFilter implements JavascriptInterface {

	public static final String INTERFACE_NAME = "PregFilter";
	@Override
	public String getInterfaceName() {
		return mBrowser.application.resources.prefix + INTERFACE_NAME;
	}
	@Override
	public JavascriptInterface getInheritInterface(Browser childBrowser) {
		return new RexseePregFilter(childBrowser);
	}
	@Override
	public JavascriptInterface getNewInterface(Browser childBrowser) {
		return new RexseePregFilter(childBrowser);
	}

	public static final String PREG_FILTER_TYPE = "preg";

	private final Browser mBrowser;
	public static boolean mIsDebuging = false;

	public RexseePregFilter(Browser browser) {
		mBrowser = browser;
	}

	//JavaScript Interface

	public void setDebuging(boolean debuging) {
		mIsDebuging = debuging;
	}
	public boolean isDebuging() {
		return mIsDebuging;
	}

	//Operation on filter
	public void add(String name) {
		PregFilterListener listener = new PregFilterListener(name);
		mBrowser.urlListeners.addFilter(listener);
	}
	public void remove(String name) {
		mBrowser.urlListeners.removeFilters(PREG_FILTER_TYPE, name);
	}
	public int size() {
		return mBrowser.urlListeners.getFilterSizeByType(PREG_FILTER_TYPE);
	}
	public String get() {
		return mBrowser.urlListeners.getFiltersByType(PREG_FILTER_TYPE);
	}
	public boolean exists(String name) {
		return mBrowser.urlListeners.getFilter(PREG_FILTER_TYPE, name) != null;
	}

	//Operation on url
	public String getUrls(String name) {
		FilterListener listener = mBrowser.urlListeners.getFilter(PREG_FILTER_TYPE, name);
		if (listener == null) return "[]";
		else return listener.getUrls();
	}
	public int getNumberOfUrls(String name) {
		FilterListener listener = mBrowser.urlListeners.getFilter(PREG_FILTER_TYPE, name);
		if (listener == null) return 0;
		else return listener.getNumberOfUrls();
	}
	public boolean clearUrls(String name) {
		FilterListener listener = mBrowser.urlListeners.getFilter(PREG_FILTER_TYPE, name);
		if (listener == null) return false;
		else {
			listener.clearUrls();
			return true;
		}
	}
	public boolean addUrl(String name, String url) {
		FilterListener listener = mBrowser.urlListeners.getFilter(PREG_FILTER_TYPE, name);
		if (listener == null) return false;
		else {
			listener.addUrl(url);
			return true;
		}
	}
	public boolean containsUrl(String name, String url) {
		FilterListener listener = mBrowser.urlListeners.getFilter(PREG_FILTER_TYPE, name);
		if (listener == null) return false;
		else return listener.containsUrl(url);
	}

	//Operation on preg pair
	public String getPairs(String name) {
		PregFilterListener listener = (PregFilterListener) mBrowser.urlListeners.getFilter(PREG_FILTER_TYPE, name);
		if (listener == null) return "[]";
		else return listener.getPairs();
	}
	public int getNumberOfPairs(String name) {
		PregFilterListener listener = (PregFilterListener) mBrowser.urlListeners.getFilter(PREG_FILTER_TYPE, name);
		if (listener == null) return 0;
		else return listener.getNumberOfPairs();
	}
	public boolean clearPairs(String name) {
		PregFilterListener listener = (PregFilterListener) mBrowser.urlListeners.getFilter(PREG_FILTER_TYPE, name);
		if (listener == null) return false;
		else {
			listener.clearPairs();
			return true;
		}
	}
	public boolean addPair(String name, String source, String replacement) {
		PregFilterListener listener = (PregFilterListener) mBrowser.urlListeners.getFilter(PREG_FILTER_TYPE, name);
		if (listener == null) return false;
		else {
			listener.addPair(new PregPair(source, replacement));
			return true;
		}
	}

}
